using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using Common;
using CustomChart;
using Infragistics.UltraChart.Resources.Appearance;
using Microsoft.Win32;
using SExperiment;

namespace MainView.StandardCurve
{
	public partial class UCStandardCurveChart : System.Windows.Controls.UserControl, IDisposable, IComponentConnector
	{
		private const string _TextFormat = "0.00000";

		private StandardChart _Chart;

		public static readonly RoutedEvent OnHighLightChangedRoutedEvent = EventManager.RegisterRoutedEvent("OnHighLightChangedRoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(UCStandardCurveChart));

		public static readonly DependencyProperty HighLightProperty = DependencyProperty.Register("HighLight", typeof(bool), typeof(UCStandardCurveChart), new UIPropertyMetadata(true));

		public static readonly DependencyProperty TargetStdCurveProperty = DependencyProperty.Register("TargetStdCurve", typeof(TargetStdCurve), typeof(UCStandardCurveChart), new UIPropertyMetadata(null, OnTargetStdCurveChanged));

		private XYSeriesEx _SeriesHighLight = new XYSeriesEx();

		private XYSeriesEx _SeriesStdLine = new XYSeriesEx();

		private XYSeriesEx _SeriesStdDot = new XYSeriesEx();

		private List<int> _HighLightList = new List<int>();

		private Hashtable hsNotStandartCurve = new Hashtable();

		public StandardChart UltraChart
		{
			get
			{
				if (windowsFormsHost.Child == null)
				{
					_Chart = new StandardChart();
					_Chart.Chart.Dock = DockStyle.Fill;
					windowsFormsHost.Child = _Chart.Chart;
				}
				return _Chart;
			}
		}

		public bool HighLight
		{
			get
			{
				return (bool)GetValue(HighLightProperty);
			}
			set
			{
				SetValue(HighLightProperty, value);
			}
		}

		public TargetStdCurve TargetStdCurve
		{
			get
			{
				return (TargetStdCurve)GetValue(TargetStdCurveProperty);
			}
			set
			{
				SetValue(TargetStdCurveProperty, value);
			}
		}

		public List<int> HighLightList
		{
			get
			{
				return _HighLightList;
			}
		}

		public event RoutedEventHandler OnHighLightChanged
		{
			add
			{
				AddHandler(OnHighLightChangedRoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnHighLightChangedRoutedEvent, value);
			}
		}

		private void RaiseHighLightChanged()
		{
			if (HighLight)
			{
				RaiseEvent(new RoutedEventArgs
				{
					RoutedEvent = OnHighLightChangedRoutedEvent
				});
			}
		}

		private static void OnTargetStdCurveChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCStandardCurveChart uCStandardCurveChart = sender as UCStandardCurveChart;
			if (uCStandardCurveChart != null)
			{
				uCStandardCurveChart.RefreshStdCurve();
			}
		}

		private void RefreshStdCurve()
		{
			_SeriesHighLight.Points.Clear();
			foreach (XYSeriesEx value in hsNotStandartCurve.Values)
			{
				value.Points.Clear();
			}
			_SeriesStdLine.Points.Clear();
			_SeriesStdDot.Points.Clear();
			UltraChart.DisplayAnnotation(false);
			if (TargetStdCurve == null)
			{
				return;
			}
			UltraChart.MinX = TargetStdCurve.StdDot.Select((Dot s) => s.X).Min() - 1.0;
			UltraChart.MaxX = TargetStdCurve.StdDot.Select((Dot s) => s.X).Max() + 1.0;
			if (TargetStdCurve.Slope < 0.0)
			{
				UltraChart.MinY = UltraChart.MaxX * TargetStdCurve.Slope + TargetStdCurve.Intercept;
				UltraChart.MaxY = UltraChart.MinX * TargetStdCurve.Slope + TargetStdCurve.Intercept;
			}
			else
			{
				UltraChart.MinY = UltraChart.MinX * TargetStdCurve.Slope + TargetStdCurve.Intercept;
				UltraChart.MaxY = UltraChart.MaxX * TargetStdCurve.Slope + TargetStdCurve.Intercept;
			}
			_SeriesStdLine.Points.Add(new XYDataPoint(UltraChart.MinX, UltraChart.MinX * TargetStdCurve.Slope + TargetStdCurve.Intercept, "", false));
			_SeriesStdLine.Points.Add(new XYDataPoint(UltraChart.MaxX, UltraChart.MaxX * TargetStdCurve.Slope + TargetStdCurve.Intercept, "", false));
			for (int i = 0; i < TargetStdCurve.StdDot.Count; i++)
			{
				XYDataPoint xYDataPoint = new XYDataPoint(TargetStdCurve.StdDot[i].X, TargetStdCurve.StdDot[i].Y, "", false);
				if (TargetStdCurve.StdDotWellNo.Count > i)
				{
					xYDataPoint.Label = TargetStdCurve.StdDotWellNo[i].ToString();
				}
				_SeriesStdDot.Points.Add(xYDataPoint);
			}
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.AppendLine(Utility.GetMessage("std_curve_slope") + Utility.GetMessage("Colon") + " " + ((TargetStdCurve.Slope >= 0.0) ? " " : "") + TargetStdCurve.Slope.ToString("0.00000"));
			stringBuilder.AppendLine(Utility.GetMessage("std_curve_intercept") + Utility.GetMessage("Colon") + " " + ((TargetStdCurve.Intercept >= 0.0) ? " " : "") + TargetStdCurve.Intercept.ToString("0.00000"));
			stringBuilder.AppendLine(Utility.GetMessage("std_curve_rvalue") + Utility.GetMessage("Colon") + " " + ((TargetStdCurve.r_Value >= 0.0) ? " " : "") + TargetStdCurve.r_Value.ToString("0.00000"));
			stringBuilder.AppendLine(Utility.GetMessage("std_curve_efficiency") + Utility.GetMessage("Colon") + " " + ((TargetStdCurve.Efficiency >= 0.0) ? " " : "") + TargetStdCurve.Efficiency.ToString("0.00000"));
			UltraChart.SetAnnotationText(stringBuilder.ToString());
			UltraChart.DisplayAnnotation();
		}

		public UCStandardCurveChart()
		{
			InitializeComponent();
			UltraChart.SetTitleX(Utility.GetMessage("Log_Co"));
			UltraChart.SetTitleY(Utility.GetMessage("Ct"));
			UltraChart.SetXFormat("0.0");
			UltraChart.SetYFormat("0.00");
			_SeriesHighLight.PEs.Add(new PaintElement());
			_SeriesHighLight.PEs[0].Fill = Color.Black;
			UltraChart.AddHighLightSeries(_SeriesHighLight);
			_SeriesStdLine.PEs.Add(new PaintElement());
			_SeriesStdLine.PEs[0].Fill = Utility.GetDrawingColor("ColorStdCurve");
			UltraChart.AddDataSeries(_SeriesStdLine);
			_SeriesStdDot.PEs.Add(new PaintElement());
			_SeriesStdDot.PEs[0].Fill = Utility.GetDrawingColor("ColorStdCurvePoint");
			UltraChart.AddScatterSeries(_SeriesStdDot);
			UltraChart.OnSelectedSeriesChanged += UltraChart_OnSelectedSeriesChanged;
			UltraChart.OnGetTooltipsArgs += UltraChart_OnGetTooltipsArgs;
			UltraChart.OnClearHighLightSeries += UltraChart_OnClearHighLightSeries;
			UltraChart.Chart.SizeChanged += Chart_SizeChanged;
			ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = Utility.GetMessage("ExportData");
			toolStripMenuItem.Click += MenuExportData_Click;
			UltraChart.InsertExportDataMenu(toolStripMenuItem);
		}

		private void MenuExportData_Click(object sender, EventArgs e)
		{
			Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
			saveFileDialog.Filter = string.Format("{0} {1}|*.{0}", "csv", Utility.GetMessage("File"));
			saveFileDialog.RestoreDirectory = true;
			saveFileDialog.FileName = ((TargetStdCurve == null) ? string.Empty : TargetStdCurve.TargetName);
			if (saveFileDialog.ShowDialog() == true)
			{
				File.WriteAllText(saveFileDialog.FileName, GetStatandCurveData(), Encoding.Default);
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_data_export_sussess"));
			}
		}

		public string GetStatandCurveData()
		{
			StringBuilder stringBuilder = new StringBuilder();
			if (TargetStdCurve != null)
			{
				stringBuilder.AppendLine(Utility.GetMessage("std_curve_slope") + "," + TargetStdCurve.Slope.ToString("0.00000"));
				stringBuilder.AppendLine(Utility.GetMessage("std_curve_intercept") + "," + TargetStdCurve.Intercept.ToString("0.00000"));
				stringBuilder.AppendLine(Utility.GetMessage("std_curve_rvalue") + "," + TargetStdCurve.r_Value.ToString("0.00000"));
				stringBuilder.AppendLine(Utility.GetMessage("std_curve_efficiency") + "," + TargetStdCurve.Efficiency.ToString("0.00000"));
				stringBuilder.AppendLine();
				stringBuilder.AppendLine(Utility.GetMessage("property") + "," + Utility.GetMessage("Ct"));
				foreach (Dot item in TargetStdCurve.StdDot.OrderBy((Dot s) => s.X))
				{
					stringBuilder.AppendLine(Math.Pow(10.0, item.X) + "," + item.Y);
				}
			}
			return stringBuilder.ToString();
		}

		public void Dispose()
		{
			UltraChart.Dispose();
			windowsFormsHost.Dispose();
		}

		private void Chart_SizeChanged(object sender, EventArgs e)
		{
			UltraChart.UpdataAnnotationLocation(UltraChart.Chart.Width - 100, 50);
		}

		private void UltraChart_OnSelectedSeriesChanged(object sender, SelectedSeriesChangedArgs e)
		{
			if (e.SelectedSeries == null)
			{
				_HighLightList = new List<int>();
				RefreshHighLightChanged(_HighLightList);
				RaiseHighLightChanged();
			}
		}

		private void UltraChart_OnGetTooltipsArgs(object sender, GetTooltipsArgs e)
		{
			_HighLightList = new List<int>();
			if (e.Series != null && e.DataPoint != null && e.DataPoint != null)
			{
				int result = -1;
				int.TryParse(e.DataPoint.Label, out result);
				if (result > 0)
				{
					_HighLightList.Add(result);
				}
			}
			RefreshHighLightChanged(_HighLightList);
			RaiseHighLightChanged();
		}

		private void UltraChart_OnClearHighLightSeries(object sender, EventArgs e)
		{
			_HighLightList = new List<int>();
			RefreshHighLightChanged(_HighLightList);
			RaiseHighLightChanged();
		}

		public void RefreshHighLightChanged(List<int> HighLightNoList)
		{
			bool highLight = false;
			if (HighLight && HighLightNoList != null && HighLightNoList.Count > 0)
			{
				List<XYDataPoint> list = new List<XYDataPoint>();
				foreach (int no in HighLightNoList)
				{
					IEnumerable<XYDataPoint> source = _SeriesStdDot.Points.Cast<XYDataPoint>();
					Func<XYDataPoint, bool> predicate = delegate(XYDataPoint s)
					{
						string label = s.Label;
						int num = no;
						return label == num.ToString();
					};
					list.AddRange(source.Where(predicate).ToList());
					foreach (XYSeriesEx value in hsNotStandartCurve.Values)
					{
						list.AddRange(value.Points.Cast<XYDataPoint>().Where(delegate(XYDataPoint s)
						{
							string label2 = s.Label;
							int num2 = no;
							return label2 == num2.ToString();
						}).ToList());
					}
				}
				_SeriesHighLight.Points.Clear();
				foreach (XYDataPoint item in list)
				{
					_SeriesHighLight.Points.Add(new XYDataPoint(item.ValueX, item.ValueY, "", false));
				}
				highLight = true;
			}
			UltraChart.AddHighLightSeries(_SeriesHighLight, highLight);
		}

		public void DisplayOtherStandardCurve(bool visible, NotStandartCurveData data)
		{
			XYSeriesEx xYSeriesEx = null;
			if (!hsNotStandartCurve.Contains(data.Color))
			{
				xYSeriesEx = new XYSeriesEx();
				xYSeriesEx.PEs.Add(new PaintElement());
				xYSeriesEx.PEs[0].Fill = data.Color;
				hsNotStandartCurve.Add(data.Color, xYSeriesEx);
				UltraChart.AddScatterSeries(xYSeriesEx);
			}
			else
			{
				xYSeriesEx = hsNotStandartCurve[data.Color] as XYSeriesEx;
			}
			xYSeriesEx.Points.Clear();
			for (int i = 0; i < data.ListPoint.Count; i++)
			{
				XYDataPoint xYDataPoint = new XYDataPoint(data.ListPoint[i].X, data.ListPoint[i].Y, "", false);
				if (data.WellID.Count > i)
				{
					xYDataPoint.Label = data.WellID[i].ToString();
				}
				xYSeriesEx.Points.Add(xYDataPoint);
			}
			xYSeriesEx.Visible = visible;
		}
	}
}
